<template>
  <div>
    <!-- 按钮 -->
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="addCategory">添加科目</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 栏目管理表格 -->
    <div>
      <el-table :data="localCategory" style="width: 100%; height:3000px;" width="formlabelwidth">
        <el-table-column label="序号" type="index" />
        <el-table-column label="科目名称" prop="name" />
        <el-table-column label="科目图片">
          <template v-slot="scope">
            <img :src="scope.row.icon" alt="" width="70px" height="70px">
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-link type="primary" style="margin-right: 10px;" @click="dateCategory(scope.row)">修改</el-link>
            <el-link type="danger" @click="deleteCategory(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 抽屉 -->
    <div>
      <el-drawer ref="myform" :title="title" :visible.sync="dialog" direction="ltr" custom-class="demo-drawer" size="50%">
        <div class="demo-drawer__content">
          <el-form :model="form">
            {{ form }}
            <el-form-item label="科目名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off" style="width: 500px;" />
            </el-form-item>
            <el-form-item label="栏目图标">
              <el-upload class="avatar-uploader" action="http://121.4.83.32:7006/UploadControlle/upload"
                :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-form>
          <div class="demo-drawer__footer" style="position: fixed; bottom: 100px; ">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="tosave">确定</el-button>
          </div>
        </div>
      </el-drawer>
    </div>

  </div>
</template>
<script>
import { get, post } from '@/utils/request.js'
export default {
  data() {
    return {
      // form: {},
      localCategory: [],
      table: false,
      dialog: false,
      loading: false,
      imageUrl: '',
      form: {
        name: '',
        icon: ''
      },
      formLabelWidth: '100px',
      timer: null,
      title: '新增科目',
      params: {
        page: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getCategory()
  },
  methods: {
    // 图片上传后
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.icon = res.data
    },
    // 图片上传前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // 查询所有栏目
    async getCategory() {
      const res = await get('/productCategory/pageQuery', this.params)
      console.log(res, '123456')
      this.localCategory = res.data.list
    },
    // 添加栏目
    addCategory() {
      this.form = {}
      this.imageUrl = ''
      this.dialog = true
      this.title = '新增栏目'
    },
    // 修改栏目
    dateCategory(row) {
      // this.imageUrl = ''
      this.form = {}
      this.dialog = true
      this.title = '修改栏目'
      this.form = { ...row }
      this.imageUrl = this.form.icon
    },
    // 保存
    tosave() {
      const res = post('/productCategory/saveOrUpdate', this.form)
      res.then((res) => {
        if (res.status == '200') {
          this.$notify({
            title: '添加成功',
            // message: res,
            type: 'success'
          })
        } else {
          this.$notify.info({
            title: '添加失败'
            // message: '这是一条消息的提示消息'
          })
        }
      })
      setTimeout(() => {
        this.dialog = false
        this.getCategory()
      }, 1000)
    },
    // 删数据
    deleteCategory(id) {
      const res = get('/productCategory/deleteById', { id })
      console.log(res)
      res.then((res) => {
        if (res.status == 200) {
          this.$notify({
            title: '删除成功',
            // message: res,
            type: 'success'
          })
        }
      }),
        setTimeout(() => {
          this.getCategory()
        }, 1000)
    },
    // 关抽屉
    close() {
      this.dialog = false
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
